package org.wmq.housewebadmin.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.wmq.housemodel.entity.Facility;
import org.wmq.housemodel.entity.Result;
import org.wmq.housewebadmin.mapper.FacilityMapper;
import org.wmq.housewebadmin.service.FacilityService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class FacilityServiceImpl implements FacilityService {
    private final FacilityMapper facilityMapper;

    @Override
    public List<Facility> getList(Byte type) {
        if (type == null){
            return this.facilityMapper.selectAll();
        }else{
            return this.facilityMapper.selectListByType(type);
        }
    }

    @Override
    public Result<Void> saveOrUpdate(Facility facility) {
        if (facility.getId() == null){
            if (this.facilityMapper.insert(facility) == 0){
                return Result.error("插入失败");
            }else{
                return Result.ok();
            }
        }else{
            if(this.facilityMapper.updateById(facility) == 0){
                return Result.error("更新失败");
            }else{
                return Result.ok();
            }
        }
    }

    @Override
    public boolean deleteById(Long id) {
        Facility facility = new Facility();
        facility.setId(id);
        return this.facilityMapper.deleteById(facility) == 1;
    }
}
